<?php


namespace App\Dao\User;


use App\Model\User\User;
use App\Model\UserBill;
use Hyperf\Database\Model\Builder;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Yj\Daos\BaseDao;
use Yj\Daos\BaseDaos;
use Yj\Daos\Verify;

/**
 * @Notes:
 * @User: zwc
 * @Date: 2022-05-11 0:03
 * @var User
 */
class UserDao extends BaseDaos
{

    public function staticsAdd(string|null $time)
    {
        return $this->tap(function ($query) use ($time) {
            $time && $query->yjRangeTime('add_time', $time);
        })->count();
    }

    public function DaoWhere(array $params)
    {
        return $this->getDaoQuery($params, function (Verify $verify) {
            $verify('id', function (Builder $query, $id) {
                $query->where('id', $id);
            });
            $verify('uid', function (Builder $query, $uid) {
                $query->where('uid', $uid);
            });
            $verify('mobile', function (Builder $query, $title) {
                $query->where('mobile', $title);
            });
        });
    }

    public function getOpenid($userId)
    {
        return $this->DaoWhere(['id' => $userId])->value('openid');
    }

    public function detail($usersId)
    {

        return $this->DaoWhere(['uid' => $usersId])->first();
    }

    /**
     * @Inject()
     * @var UserBill
     */
    protected $userBillModel;

    public static $accountType = [
        'task' => [
            'title' => '购买任务',
            'mark' => '任务付款',
        ],
        'spread' => [
            'title' => '邀请好友',
            'mark' => '邀请好友',
        ],
        'task_settlement' => [
            'title' => '任务结算',
            'mark' => '任务结算',
        ],
        'exam' => [
            'title' => '每日答题',
            'mark' => '每日答题',
        ],
        'fish' => [
            'title' => '出售鱼',
            'mark' => '出售鱼',
        ],
        'sign_task' => [
            'title' => '签到任务',
            'mark' => '签到任务',
        ],
    ];

    public function opAccount($accountType, $userId, $number, $type, $link_id = 0)
    {

        Db::beginTransaction();
        try {
            $user = $this->where('uid', $userId)->lockForUpdate()->firstOrError();
            if ($accountType == 'now_money') {
                if ($user->now_money < abs($number)) {
                    _Exception('余额不足');
                }
            }
            $user->{$accountType} += $number;
            $user->save();

            $this->userBillModel->create([
                'uid' => $userId,
                'link_id' => $link_id,
                'title' => static::$accountType[$type]['title'],
                'pm' => $number > 0 ? 1 : 0,
                'category' => $accountType,
                'type' => $type,
                'add_time' => time(),
                'number' => abs($number),
                'balance' => $user->now_money,
                'mark' => static::$accountType[$type]['mark'],
                'add_time' => time(),
            ]);

            Db::commit();
            //记录日志
        } catch (\Exception $e) {
            Db::rollBack();
            _Exception($e->getMessage());
        }
        return true;
    }


}